package com.lqjava.flowable.beans.utils;

import com.lqjava.flowable.beans.exceptions.ApiParamsException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
/**
 * 参数验证
 * 验证类型: 目前只支持必填验证
 * @author lq 2021年3月21日 18:35:33
 */
public class ParamsHelper {

	/** 手机正则*/
	public static final Pattern PHONE_PATTERN = Pattern.compile("^[1](([3|5|8|9][\\d])|([4][4,5,6,7,8,9])|([6][2,5,6,7])|([7][^9])|([9][1,8,9]))[\\d]{8}$");

	/** 身份证正则*/
	public static final Pattern IDENTITY_CARD_PATTERN = Pattern.compile("(^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}$)");
	
	/** 邮箱正则*/
	public static final Pattern EMAIL_PATTERN = Pattern.compile("^[A-Za-z0-9\\u4e00-\\u9fa5]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$");


	/**
	 * 必填验证
	 * 会验证该对象的所有get方法是否有值
	 * @param obj 需要验证的对象
	 */
	public static void requiredValidator (Object obj){
		Field[] declaredFields = obj.getClass().getDeclaredFields();
		for (int i = 0; i < declaredFields.length; i++) {
			requiredValidator(obj,declaredFields[i].getName());
		}
	}

	/**
	 * 必填验证
	 * @param obj 需要验证的对象
	 * @param propertys 该对象的属性名称
	 */
	public static void requiredValidator (Object obj, String... propertys){
		if(obj == null) ApiParamsException.throwBusiness("Valida obj missing");
		if(propertys == null) ApiParamsException.throwBusiness("Valida propertys missing");
		Class<? extends Object> clazz = obj.getClass();
		for (int i = 0; i < propertys.length; i++) {
			String property = propertys[i];
			// 内省机制获取属性信息
			PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(clazz, property);
			if(pd == null) ApiParamsException.throwBusiness("property ["+property+"] not found in" + clazz);
			Method readMethod = pd.getReadMethod();
			if(readMethod == null) ApiParamsException.throwBusiness("property ["+property+"] not have readMethod");
			Object invoke = null;
			try {
				invoke = readMethod.invoke(obj);
			} catch (Exception e) {
				ApiParamsException.throwBusiness("property ["+property+"] readMethod invoke Exception:" + e.getMessage());
			}

			if (invoke == null) ApiParamsException.throwBusiness("property ["+property+"] is required");
			if(invoke instanceof String){
				if(StringUtils.isBlank(invoke.toString())) ApiParamsException.throwBusiness("property ["+property+"] is required");
			}
			if(invoke instanceof List || invoke instanceof Set  || invoke instanceof Collection){
				if(((Collection)invoke).size() == 0) ApiParamsException.throwBusiness("property ["+property+"] is required and size > 0");
			}
			if(invoke instanceof Map){
				if(((Map)invoke).size() == 0) ApiParamsException.throwBusiness("property ["+property+"] is required and size > 0");
			}
		}
	}

}
